package henu.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import henu.javaweb.dao.DaoFactory;
import henu.javaweb.bean.Student;

/**
 * Servlet implementation class UploadStudentPasswordServlet
 */
@WebServlet("/UpdateStudentPasswordServlet")
public class UpdateStudentPasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateStudentPasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;Charset=utf-8");
		PrintWriter out=response.getWriter();
		
		String original_pass=request.getParameter("original_pass");
		String new_pass=request.getParameter("new_pass");
		String confirm_pass=request.getParameter("confirm_pass");
		
		String string=(String)request.getSession().getAttribute("sno");
		Student s=DaoFactory.getStudentDaoInstance().findBySno(string);
		boolean result=false;
		
		if(original_pass.equals(s.getS_password())||new_pass.equals(confirm_pass)){
			if(original_pass.equals(s.getS_password())) {
				if(new_pass.equals(confirm_pass)) {
					result=DaoFactory.getStudentDaoInstance()
							.updateStuPass(s.getS_no().toString(), new_pass);
					if(result) {
						out.print("<script type=\"text/javascript\">alert('修改口令成功!');history.go(-1);</script>");
						//response.sendRedirect("jsp/MainLoginPage.jsp");
					}
				}else {
					response.sendRedirect("jsp/student/confirm_error.jsp");
				}
			}else {
				response.sendRedirect("jsp/student/original_error.jsp");
			}
		}else {
			response.sendRedirect("jsp/student/all_error.jsp");
		}
		
	}

}
